home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / +system+ / tools / gui / bgui.lha / bgui / Examples / Source / ColorWheel.c < prev    next >
C/C++ Source or Header  |  2000-02-27  |  28KB  |  394 lines

  1. /*
  2.  * @(#) $Header: /cvsroot/bgui/examples/Attic/ColorWheel.c,v 1.1.2.1 1998/02/28 17:45:05 mlemos Exp $
  3.  *
  4.  * ColorWheel.c
  5.  *
  6.  * (C) Copyright 1998 Manuel Lemos.
  7.  * (C) Copyright 1995 Jaba Development.
  8.  * (C) Copyright 1995 Jan van den Baard.
  9.  * All Rights Reserved.
  10.  *
  11.  * $Log: ColorWheel.c,v $
  12.  * Revision 1.1.2.1  1998/02/28 17:45:05  mlemos
  13.  * Ian sources
  14.  *
  15.  *
  16.  */
  17.  
  18. /* Execute me to compile with DICE V3.0
  19. dcc ColorWheel.c -proto -mi -ms -mRR -3.0 -lbgui
  20. quit
  21. */
  22.  
  23. /*
  24.  *      This example is based on the original ColorWheel.c
  25.  *      code by Commodore.
  26.  */
  27.  
  28. #include "DemoCode.h"
  29.  
  30. #include <dos/dos.h>
  31. #include <gadgets/colorwheel.h>
  32. #include <gadgets/gradientslider.h>
  33. #include <graphics/displayinfo.h>
  34.  
  35. #include <clib/colorwheel_protos.h>
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39.  
  40. /*
  41. **      Library base pointers.
  42. **/
  43. struct Library *ColorWheelBase = NULL, *GradientSliderBase = NULL;
  44.  
  45. /*
  46. **      Object ID's
  47. **/
  48. #define ID_QUIT                 1
  49. #define ID_WHEEL                2
  50.  
  51. /*
  52. **      Structure for LoadRGB32().
  53. **/
  54. struct load32 {
  55.         UWORD           l32_len;
  56.         UWORD           l32_pen;
  57.         ULONG           l32_red;
  58.         ULONG           l32_grn;
  59.         ULONG           l32_blu;
  60. };
  61.  
  62. /*
  63. **      Ensure correct operation on ECS.
  64. **/
  65. #define GRADCOLORS 4
  66.  
  67. /*
  68. **      The program even does version checking... sjeez...
  69. **/
  70. extern struct Library *SysBase;
  71.  
  72. /*
  73. **      And were in...
  74. **/
  75. VOID StartDemo( void )
  76. {
  77.         Object                  *WN_Window, *GA_Quit, *GA_Wheel, *GA_GrSlider, *GA_Master, *RealSlider = NULL;
  78.         struct Window           *win;
  79.         struct Screen           *scr;
  80.         ULONG                   winsig = 0L, sigrec, rc;
  81.         BOOL                    running = TRUE;
  82.         ULONG                   colortable[ 96 ];
  83.         struct load32           color_list[ GRADCOLORS + 1 ];
  84.         WORD                    penns[ GRADCOLORS + 1 ];
  85.         struct ColorWheelRGB    rgb;
  86.         struct ColorWheelHSB    hsb;
  87.         ULONG                   modeID = HIRES_KEY;
  88.         UWORD                   numPens, i;
  89.         DisplayInfoHandle       displayhandle;
  90.         struct DimensionInfo    dimensioninfo;
  91.  
  92.         /*
  93.         **      Need OS 3.0.
  94.         **/
  95.         if ( SysBase->lib_Version < 39 ) {
  96.                 Tell( "OS 3.0 required!" );
  97.                 exit( 0 );
  98.         }
  99.  
  100.         /*
  101.         **      Open the libraries. Get display information etc...
  102.         **/
  103.         if ( ColorWheelBase = OpenLibrary( "gadgets/colorwheel.gadget", 39L )) {
  104.                 if ( GradientSliderBase = OpenLibrary( "gadgets/gradientslider.gadget", 39L )) {
  105.                         if ( displayhandle = FindDisplayInfo( modeID )) {
  106.                                 if ( GetDisplayInfoData( displayhandle, ( UBYTE * )&dimensioninfo, sizeof( struct DimensionInfo ), DTAG_DIMS, NULL )) {
  107.                                         /*
  108.                                         **      Open up a screen with as many
  109.                                         **      colors as possible. Also make it
  110.                                         **      like your workbench screen.
  111.                                         **/
  112.                                         if ( scr = OpenScreenTags( NULL, SA_Depth,              dimensioninfo.MaxDepth,
  113.                                                                          SA_SharePens,          TRUE,
  114.                                                                          SA_LikeWorkbench,      TRUE,
  115.                                                                          SA_Interleaved,        TRUE,
  116.                                                                          SA_Title,              "ColorWheel Screen",
  117.                                                                          TAG_END )) {
  118.                                                 /*
  119.                                                 **      Get the colors.
  120.                                                 **/
  121.                                                 GetRGB32( scr->ViewPort.ColorMap, 0L, 32L, colortable );
  122.  
  123.                                                 /*
  124.                                                 **      Setup gradient slider
  125.                                                 **      as color 0.
  126.                                                 **/
  127.                                                 rgb.cw_Red   = colortable[ 0 ];
  128.                                                 rgb.cw_Green = colortable[ 1 ];
  129.                                                 rgb.cw_Blue  = colortable[ 2 ];
  130.  
  131.                                                 /*
  132.                                                 **      Convert the RGB values to
  133.                                                 **      HSB values.
  134.                                                 **/
  135.                                                 ConvertRGBToHSB( &rgb, &hsb );
  136.  
  137.                                                 /*
  138.                                                 **      Maximum brightness.
  139.                                                 **/
  140.                                                 hsb.cw_Brightness = 0xffffffff;
  141.  
  142.                                                 numPens = 0;
  143.  
  144.                                                 /*
  145.                                                 **      Build the pen array for the
  146.                                                 **      gradient slider (4 pens).
  147.                                                 **/
  148.                                                 while ( numPens < GRADCOLORS ) {
  149.                                                         /*
  150.                                                         **      Compute dim level of this pen.
  151.                                                         **/
  152.                                                         hsb.cw_Brightness = ( ULONG )0xffffffff - ((( ULONG )0xffffffff / GRADCOLORS ) * numPens );
  153.                                                         /*
  154.                                                         **      Convert this dim level
  155.                                                         **      to RGB values.
  156.                                                         **/
  157.                                                         ConvertHSBToRGB( &hsb, &rgb );
  158.                                                         /*
  159.                                                         **      Allocate a pen.
  160.                                                         **/
  161.                                                         if (( penns[ numPens ] = ObtainPen( scr->ViewPort.ColorMap, -1, rgb.cw_Red, rgb.cw_Green, rgb.cw_Blue, PEN_EXCLUSIVE )) == -1 )
  162.                                                                 break;
  163.                                                         /*
  164.                                                         **      Stuff it in the color list.
  165.                                                         **/
  166.                                                         color_list[ numPens ].l32_len = 1;
  167.                                                         color_list[ numPens ].l32_pen = penns[ numPens ];
  168.                                                         /*
  169.                                                         **      Next...
  170.                                                         **/
  171.                                                         numPens++;
  172.                                                 }
  173.  
  174.                                                 /*
  175.                                                 **      Terminate pen array and
  176.                                                 **      color list.
  177.                                                 **/
  178.                                                 penns[ numPens ]              = ~0;
  179.                                                 color_list[ numPens ].l32_len = 0;
  180.  
  181.                                                 /*
  182.                                                 **      Create the gradient slider here
  183.                                                 **      _with_ the EXT_NoRebuild tag set
  184.                                                 **      to TRUE. This _must_ be set to
  185.                                                 **      TRUE otherwise the external class
  186.                                                 **      will rebuild the object after each
  187.                                                 **      re-size resulting in colourful
  188.                                                 **      crashes when it gets attached to
  189.                                                 **      the colorwheel :)
  190.                                                 **/
  191.                                                 GA_GrSlider = ExternalObject,
  192.                                                         EXT_MinWidth,           10,
  193.                                                         EXT_MinHeight,          10,
  194.                                                         EXT_ClassID,            "gradientslider.gadget",
  195.                                                         EXT_NoRebuild,          TRUE,
  196.                                                         GRAD_PenArray,          penns,
  197.                                                         PGA_Freedom,            LORIENT_VERT,
  198.                                                         TAG_END );
  199.  
  200.                                                 /*
  201.                                                 **      Pick up a pointer to the
  202.                                                 **      "real" gradient slider object.
  203.                                                 **      This will be the pointer we
  204.                                                 **      pass to the colorwheel.
  205.                                                 **/
  206.                                                 if ( GA_GrSlider )
  207.                                                         GetAttr( EXT_Object, GA_GrSlider, ( ULONG * )&RealSlider );
  208.  
  209.                                                 /*
  210.                                                 **      Create a small window.
  211.                                                 **/
  212.                                                 WN_Window = WindowObject,
  213.                                                         WINDOW_Title,           "ColorWheel",
  214.                                                         WINDOW_RMBTrap,         TRUE,
  215.                                                         WINDOW_Screen,          scr,
  216.                                                         WINDOW_ScaleWidth,      20,
  217.                                                         WINDOW_ScaleHeight,     20,
  218.                                                         WINDOW_AutoAspect,      TRUE,
  219.                                                         /*
  220.                                                         **      The colorwheel is really slow :(
  221.                                                         **/
  222.                                                         WINDOW_NoBufferRP,      TRUE,
  223.                                                         WINDOW_MasterGroup,
  224.                                                                 GA_Master = VGroupObject, HOffset( 4 ), VOffset( 4 ), Spacing( 4 ),
  225.                                                                         StartMember, TitleSeperator( "Wheel & Slider" ), EndMember,
  226.                                                                         StartMember,
  227.                                                                                 HGroupObject, Spacing( 4 ),
  228.                                                                                         StartMember,
  229.                                                                                                 /*
  230.                                                                                                 **      The EXT_NoRebuild tag may _not_ be set to
  231.                                                                                                 **      TRUE for a colorwheel object. This is due
  232.                                                                                                 **      to the fact that colorwheels cannot change
  233.                                                                                                 **      size. This is also the reason why we need
  234.                                                                                                 **      to track the attributes of the colorwheel.
  235.                                                                                                 **/
  236.                                                                                                 GA_Wheel = ExternalObject,
  237.                                                                                                         EXT_MinWidth,           30,
  238.                                                                                                         EXT_MinHeight,          30,
  239.                                                                                                         EXT_ClassID,            "colorwheel.gadget",
  240.                                                                                                         WHEEL_Screen,           scr,
  241.                                                                                                         /*
  242.                                                                                                         **      Pass a pointer to the "real" gradient slider
  243.                                                                                                         **      here.
  244.                                                                                                         **/
  245.                                                                                                         WHEEL_GradientSlider,   RealSlider,
  246.                                                                                                         WHEEL_Red,              colortable[ 0 ],
  247.                                                                                                         WHEEL_Green,            colortable[ 1 ],
  248.                                                                                                         WHEEL_Blue,             colortable[ 2 ],
  249.                                                                                                         GA_FollowMouse,         TRUE,
  250.                                                                                                         GA_ID,                  ID_WHEEL,
  251.                                                                                                         /*
  252.                                                                                                         **      These attributes of the colorwheel are
  253.                                                                                                         **      tracked and reset to the object after
  254.                                                                                                         **      it has been rebuild. This way the current
  255.                                                                                                         **      colorwheel internals will not be lost
  256.                                                                                                         **      after the object is re-build.
  257.                                                                                                         **/
  258.                                                                                                         EXT_TrackAttr,          WHEEL_Red,
  259.                                                                                                         EXT_TrackAttr,          WHEEL_Green,
  260.                                                                                                         EXT_TrackAttr,          WHEEL_Blue,
  261.                                                                                                         EXT_TrackAttr,          WHEEL_Hue,
  262.                                                                                                         EXT_TrackAttr,          WHEEL_Saturation,
  263.                                                                                                         EXT_TrackAttr,          WHEEL_Brightness,
  264.                                                                                                 EndObject,
  265.                                                                                         EndMember,
  266.                                                                                         /*
  267.                                                                                         **      Add the externalclass object of the
  268.                                                                                         **      gradient slider here. Right next to
  269.                                                                                         **      the colorwheel :)
  270.                                                                                         **/
  271.                                                                                         StartMember,
  272.                                                                                                 GA_GrSlider, FixWidth( 20 ),
  273.                                                                                         EndMember,
  274.                                                                                 EndObject,
  275.                                                                         EndMember,
  276.                                                                         StartMember, HorizSeperator, EndMember,
  277.                                                                         StartMember,
  278.                                                                                 HGroupObject,
  279.                                                                                         VarSpace( DEFAULT_WEIGHT ),
  280.                                                                                         StartMember, GA_Quit = KeyButton( "_Quit", ID_QUIT ), EndMember,
  281.                                                                                         VarSpace( DEFAULT_WEIGHT ),
  282.                                                                                 EndObject, FixMinHeight,
  283.                                                                         EndMember,
  284.                                                                 EndObject,
  285.                                                 EndObject;
  286.  
  287.                                                 if ( WN_Window ) {
  288.                                                         /*
  289.                                                         **      Make button selectable by the keyboard.
  290.                                                         **/
  291.                                                         GadgetKey( WN_Window, GA_Quit, "q" );
  292.                                                         /*
  293.                                                         **      Open up the window.
  294.                                                         **/
  295.                                                         if ( win = WindowOpen( WN_Window )) {
  296.                                                                 /*
  297.                                                                 **      Obtain window sigmask.
  298.                                                                 **/
  299.                                                                 GetAttr( WINDOW_SigMask, WN_Window, &winsig );
  300.                                                                 /*
  301.                                                                 **      Wait for messages.
  302.                                                                 **/
  303.                                                                 do {
  304.                                                                         sigrec = Wait( winsig );
  305.  
  306.                                                                         /*
  307.                                                                         **      Window signal?
  308.                                                                         **/
  309.                                                                         if ( sigrec & winsig ) {
  310.                                                                                 while ( WN_Window && (( rc = HandleEvent( WN_Window )) != WMHI_NOMORE )) {
  311.                                                                                         switch ( rc ) {
  312.  
  313.                                                                                                 case    WMHI_CLOSEWINDOW:
  314.                                                                                                 case    ID_QUIT:
  315.                                                                                                         /*
  316.                                                                                                         **      The end.
  317.                                                                                                         **/
  318.                                                                                                         running = FALSE;
  319.                                                                                                         break;
  320.  
  321.                                                                                                 case    ID_WHEEL:
  322.                                                                                                         /*
  323.                                                                                                         **      Obtain the wheel it's HSB settings.
  324.                                                                                                         **/
  325.                                                                                                         GetAttr( WHEEL_HSB, GA_Wheel, ( ULONG * )&hsb );
  326.  
  327.                                                                                                         i = 0;
  328.  
  329.                                                                                                         /*
  330.                                                                                                         **      Recompute the gradient slider
  331.                                                                                                         **      dim colors for the current
  332.                                                                                                         **      colorwheel setting.
  333.                                                                                                         **/
  334.                                                                                                         while ( i < numPens ) {
  335.                                                                                                                 /*
  336.                                                                                                                 **      Compute dim level of this pen.
  337.                                                                                                                 **/
  338.                                                                                                                 hsb.cw_Brightness = ( ULONG )0xffffffff - ((( ULONG )0xffffffff / numPens ) * i );
  339.                                                                                                                 /*
  340.                                                                                                                 **      Convert to RGB values.
  341.                                                                                                                 **/
  342.                                                                                                                 ConvertHSBToRGB(&hsb,&rgb);
  343.                                                                                                                 /*
  344.                                                                                                                 **      Setup the pens in the color list.
  345.                                                                                                                 **/
  346.                                                                                                                 color_list[ i ].l32_red = rgb.cw_Red;
  347.                                                                                                                 color_list[ i ].l32_grn = rgb.cw_Green;
  348.                                                                                                                 color_list[ i ].l32_blu = rgb.cw_Blue;
  349.                                                                                                                 /*
  350.                                                                                                                 **      Next...
  351.                                                                                                                 **/
  352.                                                                                                                 i++;
  353.                                                                                                         }
  354.                                                                                                         /*
  355.                                                                                                         **      Show the changes in the gradient slider.
  356.                                                                                                         **/
  357.                                                                                                         LoadRGB32( &scr->ViewPort, ( ULONG * )color_list );
  358.                                                                                                         break;
  359.                                                                                         }
  360.                                                                                 }
  361.                                                                         }
  362.                                                                 } while ( running );
  363.                                                         }
  364.                                                         /*
  365.                                                         **      Kill the window.
  366.                                                         **/
  367.                                                         DisposeObject( WN_Window );
  368.                                                 }
  369.                                                 /*
  370.                                                 **      Release the allocated pens.
  371.                                                 **/
  372.                                                 while ( numPens > 0 ) {
  373.                                                         numPens--;
  374.                                                         ReleasePen( scr->ViewPort.ColorMap, penns[ numPens ] );
  375.                                                 }
  376.                                                 /*
  377.                                                 **      Close the screen.
  378.                                                 **/
  379.                                                 CloseScreen( scr );
  380.                                         }
  381.                                 }
  382.                         }
  383.                         /*
  384.                         **      Close the gradient.gadget.
  385.                         **/
  386.                         CloseLibrary( GradientSliderBase );
  387.                 }
  388.                 /*
  389.                 **      Close the colorwheel.gadget.
  390.                 **/
  391.                 CloseLibrary( ColorWheelBase );
  392.         }
  393. }
  394.